package models;

import java.util.List;

import javax.persistence.Entity;

import jiyici.Constant.ModelColumn;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

import play.db.ebean.Model;

@Entity
public class Category extends ServerBaseModel {

    private static final long serialVersionUID = 4482487573155059686L;

    public String name;

    public String displayOrder;

    @Override
    public ObjectNode toJson() {
        ObjectNode node = super.toJson();
        node.put(ModelColumn.CATEGORY_COLUMN_NAME, name);
        node.put(ModelColumn.CATEGORY_COLUMN_DISPLAY_ORDER, displayOrder);
        return node;
    }

    @Override
    public void fromJson(JsonNode json) {
        super.fromJson(json);
        name = json.findPath(ModelColumn.CATEGORY_COLUMN_NAME).getTextValue();
        displayOrder = json.findPath(ModelColumn.CATEGORY_COLUMN_DISPLAY_ORDER).getTextValue();
    }

    public static Model.Finder<Long, Category> find = new Model.Finder<Long, Category>(Long.class, Category.class);

    public static List<Category> allList(User user) {
        return find.where().eq("userId", user.id).findList();
    }

    public static List<Category> modifiedAfter(User user, long time) {
        return find.where().eq("modelId.userId", user.id).gt(ModelColumn.BASE_COLUMN_LASTMODIFIED, time)
                .findList();
    }

    public static Category addCategory(User user, String name, String displayOrder) {
        Category category = new Category();
        category.setUserId(user.id);
        category.name = name;
        category.displayOrder = displayOrder;
        category.save();
        return category;
    }

    public static void removeCategory(long id) {
        find.ref(id).delete();
    }

    // public static Category editCategory(Category category) {
    //
    // }
}
